package jrmob.yonyou.com.commonlib.app_config_tool.tool.base;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * 描述:网络工具类
 * 
 * @下午10:30:02
 * @author zhangg
 */
public class NetUtil {

	/**
	 * 判断网络是否连接
	 * 
	 * @return
	 */
	public static boolean isNetConnected(Context context) {
		ConnectivityManager cm = (ConnectivityManager)context.getSystemService(
				Context.CONNECTIVITY_SERVICE);
		if (cm == null) {
			return false;
		}
		NetworkInfo[] infos = cm.getAllNetworkInfo();
		if (infos != null) {
			for (NetworkInfo ni : infos) {
				if (ni.isConnected()) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 判断网络是否连接有线网络（ Ethernet）
	 * 
	 * @return
	 */
	private static boolean isEthernetEnabled() {
		ConnectivityManager cm = (ConnectivityManager) UiUtils.getContext().getSystemService(
				Context.CONNECTIVITY_SERVICE);
		if (cm != null) {
			NetworkInfo networkInfo = cm.getActiveNetworkInfo();
			if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_ETHERNET) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 判断当个网络是否可用
	 * 
	 * @param
	 * @return
	 */
	public static boolean isAvailable() {
		// 需要权限
		// <uses-permission
		// android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
		ConnectivityManager cwjManager = (ConnectivityManager) UiUtils.getContext()
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		return cwjManager.getActiveNetworkInfo().isAvailable();
	}


	public static  boolean isNetworkConnected(Context context) {
		if (context != null) {
			ConnectivityManager mConnectivityManager = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
			if (mNetworkInfo != null) {
				return mNetworkInfo.isAvailable();
			}
		}
		return false;
	}

}